package com.sduept.zj103.model;

import com.google.common.io.LittleEndianDataInputStream;
import com.sduept.tool.TimeTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.*;

public class FileData implements Serializable{
    private static final long serialVersionUID = -8159433382170825236L;
    private Logger logger = LoggerFactory.getLogger(FileData.class);
    public int rii;
    public int cot;
    public boolean finishFlag;
    public String fileName;
    public int fileSize;

    /*分帧传输计数器，比如传完第一帧存储put(0, 100), 传完第二帧存储put(100, 200)
    当收到最后一帧时，检查该map。看是否有遗漏的帧，若有，则重新索要遗漏的帧，正常是
    连续的，最后一个key对应的value为fileSize
     */
    public Map<Integer, Integer> transCountMap;
    public byte[] data;
//    public int realFileSize;
//    public boolean isFileSizeValid; //若为true,采用fileSize，若为false，采用realFileSize
    public static final int DEFAULT_SIZE = 20 * 1000 * 1000; //20M缓冲区

    public FileData() {
        rii = 0;
        cot = 0;
        finishFlag = false;
        fileSize = 0;
        transCountMap = new HashMap<>();
//        realFileSize = 0;
//        isFileSizeValid = true;
    }

    public List<LbFile> decodeLbFile() {
        int currentFileSize = this.fileSize;
        List<LbFile> lbFileList = new ArrayList<>();

        LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream(new ByteArrayInputStream(this.data));
        try {
            byte[] tmpFileName = new byte[40];
            littleEndianDataInputStream.read(tmpFileName);
            String fileName = new String(tmpFileName, Charset.forName("utf-8"));
            fileName = fileName.trim();

            byte[] timeStamp = new byte[7];
            littleEndianDataInputStream.read(timeStamp);
            Date pickTime = TimeTool.getDateByCp56(timeStamp);
            currentFileSize -= 47;
            while (currentFileSize > 0) {
                LbFile lbFile = new LbFile();
                lbFile.fileName = fileName;
                lbFile.faultTime = pickTime;
                lbFile.fileType = littleEndianDataInputStream.readUnsignedByte();
                lbFile.fileSize = littleEndianDataInputStream.readInt();
                lbFile.data = new byte[lbFile.fileSize];
                littleEndianDataInputStream.read(lbFile.data);
                currentFileSize -= (lbFile.fileSize + 5);
                lbFileList.add(lbFile);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return lbFileList;
    }

    public List<LbRecordFile> decodeLbRecordFile() {
        int currentFileSize = this.fileSize;
        List<LbRecordFile> lbRecordFileList = new ArrayList<>();

        LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream(new ByteArrayInputStream(this.data));
        try {
            byte[] tmpFileName = new byte[40];
            littleEndianDataInputStream.read(tmpFileName);
            String fileName = new String(tmpFileName, Charset.forName("utf-8"));
            fileName = fileName.trim();
            byte[] tmpLineName = new byte[40];
            littleEndianDataInputStream.read(tmpLineName);
            String lineName = new String(tmpLineName, Charset.forName("utf-8"));
            lineName = lineName.trim();
            int fpt = littleEndianDataInputStream.readByte();
            int jpt = littleEndianDataInputStream.readByte();
            float scl = littleEndianDataInputStream.readFloat();
            int dpi = littleEndianDataInputStream.readByte();
            int reclosureTimeMs = littleEndianDataInputStream.readShort();

            byte[] timeStamp = new byte[7];
            littleEndianDataInputStream.read(timeStamp);
            Date pickTime = TimeTool.getDateByCp56(timeStamp);
            currentFileSize -= 96;
            while (currentFileSize > 0) {
                LbRecordFile lbRecordFile = new LbRecordFile();
                lbRecordFile.fileName = fileName;
                lbRecordFile.primaryDevName = lineName; //故障（一次）设备名称，如线路名称
                lbRecordFile.fpt = fpt; //故障相别及类型
                lbRecordFile.jpt = jpt; //跳闸相别
                lbRecordFile.scl = scl; //故障距离
                lbRecordFile.dpi = dpi; //重合闸
                lbRecordFile.reclosureTimeMs = reclosureTimeMs; //重合闸时间毫秒
                lbRecordFile.startTime = pickTime;

                lbRecordFile.fileType = littleEndianDataInputStream.readUnsignedByte();
                lbRecordFile.fileSize = littleEndianDataInputStream.readInt();
                lbRecordFile.data = new byte[lbRecordFile.fileSize];
                littleEndianDataInputStream.read(lbRecordFile.data);
                currentFileSize -= (lbRecordFile.fileSize + 5);
                lbRecordFileList.add(lbRecordFile);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return lbRecordFileList;
    }

}
